<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of owloption
 *
 * @author trung
 */
class Owl_Option extends Owl_Db {
    //put your code here
    private $options_cache;
    private $no_options_cache;

    function Owl_Option(){
        $this->Owl_Db('option');
    }

    function get_all_options(){
        $option = $this->get_results('//option');
        $all_option = array();
        if ($option){            
            foreach ( (array) $option as $o) {
                $all_option[$o['name']] = $o['value'];
            }
            $this->options_cache = $all_option;
        }
        return $all_option;
    }

    function get_option($name, $default = false){
        
         if (is_array ($this->no_options_cache) && isset ($this->no_options_cache[$name])){
                return $default;
         }

         if (!isset ($this->options_cache)){
             $this->get_all_options();
         }

         if (isset ($this->options_cache[$name])){
             $value = $this->options_cache[$name];
         } else {
             $row = $this->get_row("//option[name=$name]");
             if (is_array($row)){
                 $value = $row['option_value'];
                 $this->options_cache[$name] = $value;
             } else {
                 $this->no_options_cache[$name] = true;
                 return $default;
             }
         }
         return Owl_System_Helper::unserialize($value);
     }

     function add_option($name, $value = ''){            

          if (!is_array($this->no_options_cache) || !isset ($this->no_options_cache[$name])){
              if (false !== $this->get_option($name)){
                $this->last_error= new Owl_Error("501","Option is exists");
                return false;
              }
          }

          $value = Owl_System_Helper::serialize($value);

          if ($this->insert('/options', 'option', array('name'=>$name,'value'=>$value))){
              $this->options_cache[$name] = $value;
              if ( is_array($this->no_options_cache) && isset ($this->no_options_cache[$name])){
                  unset ($this->no_options_cache[$name]);
              }
              return true;
          } else {              
              return false;
          }
          
      }

      function update_option($name, $newvalue){            
          $oldvalue = $this->get_option($name);
          
          if ($newvalue == $oldvalue){              
              return true;
          }

          if (false === $oldvalue){
              return $this->add_option($name,$newvalue);
          }

          $newvalue = Owl_System_Helper::serialize($newvalue);
          
          if($this->update('/options/option[name = "'.$name.'"]', 'option', array('name'=>$name,'value'=>$newvalue))){
              if ( is_array($this->no_options_cache) && isset ($this->no_options_cache[$name])){
                  unset ($this->no_options_cache[$name]);
              }
              $this->options_cache[$name] = $newvalue;
              return true;              
          } else {              
              return false;
              
          }
          
       }
       
      function delete_option($name){
          
          if ($this->get_option($name) === false ){
              $this->last_error = new Owl_Error("501","Option is not exists");
              return false;
          }
          if ($this->delete("/options/option[name='$name']")){
              if ( isset( $this->options_cache[$name] ) ) {
			unset( $this->options_cache[$name] );
              }
              return true;
          } else {              
              return false;
          }
          
      }      
}

//API

function get_option($name, $default = false) {
    global $owl_option;
    return $owl_option->get_option($name, $default);
}

function update_option($name, $newvalue) {
    global $owl_option;
    return $owl_option->update_option($name, $newvalue);
}

function delete_option($name) {
    global $owl_option;
    return $owl_option->delete_option($name);
}

function update_options_list($options_list) {
    foreach ((array)$options_list as $name => $newvalue) {
        if (!update_option($name, $newvalue)) {
            return false;
        }
    }
    return true;
}

function get_option_error() {
    global $owl_option;
    return $owl_option->get_error();
}
